package rs.actor

import akka.actor.typed.Behavior
import akka.actor.typed.scaladsl.{ActorContext, Behaviors}
import com.typesafe.scalalogging.Logger
import rs.dev.{I2cDJ, I2cDev}

import scala.concurrent.{ExecutionContext, Future}

object TripodI2C{

  def apply():Behavior[TripodCommand] = {
    Behaviors.setup[TripodCommand]( ctx => new TripodI2C(ctx).ready())
  }
}

class TripodI2C(ctx: ActorContext[TripodCommand]) {

  given ctx1: ActorContext[TripodCommand] = ctx
  given executionContext :ExecutionContext = ctx.executionContext

  val log = Logger(getClass)

  val pitchDj = new I2cDJ(0, 90, 0 ,180)
  val directDj = new I2cDJ(1, 90, 0 ,180)

  val pitchAcc = 1
  val directAcc = 1

  def ready(): Behavior[TripodCommand] = Behaviors.receive { (ctx, msg: TripodCommand) =>
    log.debug(s"get tripod command : ${msg}")
    msg match {
      case TripodInfo(pitch, direction) => {
        pitchDj.setDegree(pitch)
        directDj.setDegree(direction)
      }
      case TripodVelocity(pitchingVelocity, directionVelocity) => {
        pitchDj.startRotate(pitchingVelocity)
        directDj.startRotate(directionVelocity)
      }
      case TripodPitching(value) => pitchDj.startRotate(Math.signum(value)* pitchAcc)
      case TripodDirection(value) => directDj.startRotate(-1 * Math.signum(value)* directAcc)
    }
    Behaviors.same
  }
}


